package com.iwown.sample4GApi.calculation;

import com.google.protobuf.InvalidProtocolBufferException;
import com.iwown.sample4GApi.protobuf.HisDataOuterClass;
import com.iwown.sample4GApi.protobuf.HisRriData;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.List;

public class RriPreprocessor {
    private static final Logger log = LoggerFactory.getLogger(RriPreprocessor.class);

    public void DataPrepare(byte[] payload) {
        HisDataOuterClass.HisNotification hisNotify = null;
        try {
            hisNotify = HisDataOuterClass.HisNotification.parseFrom(payload);
        } catch (InvalidProtocolBufferException ex) {
            ex.printStackTrace();
            return;
        }
        if(hisNotify.hasHisData()){
            HisDataOuterClass.HisData hisData = hisNotify.getHisData();
            if(hisData.hasRri()){
                HisRriData.HisDataRRI rriData = hisData.getRri();
                /*
                    not like ecg, rri is not one time measure, but
                    continuous value, you can calculate af result
                    of random time range, combine all rri value of
                    the time range for calculation
                 */
                List<Integer> rawDataList = rriData.getRawDataList();
                List<Short> actualDataList = new ArrayList<>();
                for(int i=0;i<rawDataList.size();i++){
                    short fval = (short)((rawDataList.get(i) >> 16) & 0x0000ffff);
                    short sval = (short)(rawDataList.get(i) & 0x0000ffff);
                    actualDataList.add(fval);
                    actualDataList.add(sval);
                }
                log.info(String.format("rri count:%d",actualDataList.size()));
            }
        }
    }
}
